package com.homestead.aspect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author yhb
 * @date 2021-03-12 22:21
 */
public class RedisInvocationHandler implements InvocationHandler {
    Object targetObj;

    private boolean canOperateRedis = true;

    public RedisInvocationHandler(Object targetObj) {
        this.targetObj = targetObj;
    }

    public <V> V getProxy() {
        @SuppressWarnings("unchecked")
        V v = (V) Proxy.newProxyInstance(targetObj.getClass().getClassLoader(), targetObj.getClass().getInterfaces(), this);
        return v;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        // 能操作redis
        if (canOperateRedis) {
            result = method.invoke(targetObj, args);
        }

        return result;
    }
}
